﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WeiSiCopy 
{
    public class FileHandle
    {
        private Apply m_apply { get; set; }

        public FileHandle()
        {
        }
        public FileHandle(Apply apply)
        {
            m_apply = apply;
        }

        public List<Apply> ReadToApply(FileSystemInfo info)
        {
            List<Apply> list = null;
            if (! File.Exists(info.FullName))
            {
                Log.WriteLog(info.FullName+ " is not found", true);
                return null;
            }

            try
            {
                System.IO.StreamReader fileStream;

                fileStream = new System.IO.StreamReader(info.FullName, System.Text.Encoding.Default);

                string Line = "";
                if (fileStream.EndOfStream)
                {
                    Log.WriteLog(info.FullName +" is empty", true);
                }
                while (!fileStream.EndOfStream)
                {
                    Apply apply = null;
                    Line = fileStream.ReadLine();
                    if (Line.IndexOf("&") > 0)
                    {
                        string[] temp = Line.Split("&".ToCharArray());
                        if (temp.Length == 2)
                        {
                            apply = new Apply(BaseSetting.DBConn);
                            apply.SrcSiName = temp[0];
                            apply.DesSiName = temp[1];
                            if (apply.IsExist())
                            {
                                apply.status = 1;
                                apply.Update();
                            }
                            else
                            {

                                Log.WriteLog(apply.DesSiName + " is not apply ", true);
                                apply = null;
                            }
                        }
                        else
                        {
                            Log.WriteLog(Line + " format error", true);
                        }
                    }
                    if (apply != null)
                    {
                        if (list == null)
                        {
                            list = new List<Apply>();
                        }
                        list.Add(apply);
                    }
                   


                }
                fileStream.Close();



                FileInfo df = new FileInfo(info.FullName);
                df.Wei_MoveTo(BaseSetting.DesPath, info);



            }
            catch (Exception ex)
            {
                Log.WriteLog("ReadToApply is error " +ex.ToString() , true);
            }

            return list;

        }
        private string Replace(string line)
        {
            if (line.IndexOf("$sitename") > 0)
            {
                return "	var $sitename = '" + m_apply.ApplyName + "';";
            }
            else
            {
                List<string> list = line.GetParser();
                if (list.Count == 4)
                {
                    return "	var $" + list[1] + " = " + list[2].Replace(m_apply.SrcSiName, m_apply.DesSiName)+ ";"; 
                }

            }

            return line.Replace(m_apply.SrcSiName, m_apply.DesSiName);
        }
        public bool ModitfyConfig(string fullname)
        {
            bool flag = false;
            if (!File.Exists(fullname))
            {
                Log.WriteLog(fullname + " is not found", true);
                return flag;
            }
            try
            {
                System.IO.StreamReader fileStream;

                fileStream = new System.IO.StreamReader(fullname, System.Text.Encoding.UTF8);

                if (fileStream.EndOfStream)
                {
                    Log.WriteLog(fullname + " is empty", true);
                }
                List<string> list = null;
                while (!fileStream.EndOfStream)
                {
                    if (list == null)
                    {
                        list = new List<string>();
                    }
                    list.Add(fileStream.ReadLine());

                }
                fileStream.Close();

                if (list != null)
                {
                    StreamWriter swFile = null;
                    try
                    {
                        swFile = new StreamWriter(fullname, false, new UTF8Encoding(false));
                        //foreach (string line in list)
                        for (int i=0; i< list.Count ;i++)
                        {
                            if (i != list.Count - 1)
                            {
                                swFile.WriteLine(Replace(list[i]));
                            }
                            else
                            {
                                swFile.Write(Replace(list[i]));
                            }
                        }
                        

                        swFile.Close();
                        swFile = null;

                        flag = true;
                    }
                    catch (Exception ex)
                    {
                        string aa = ex.ToString();
                    }
                    finally
                    {
                        if (swFile != null)
                        {
                            swFile.Close();
                            swFile = null;
                        }
                    }
                }
                else
                {
                    Log.WriteLog(fullname + " config is empty ", true);
                }


            }
            catch (Exception ex)
            {
                Log.WriteLog(fullname + " ModitfyConfig is error " + ex.ToString(), true);
            }

            return flag;
        }
    }
}
